export BUILD_TOPDIR=$(PWD)
export STAGING_DIR=$(BUILD_TOPDIR)/tmp
export TOPDIR=$(PWD)
export UBOOTDIR=$(TOPDIR)/u-boot

### Toolchain config ###
CONFIG_TOOLCHAIN_PREFIX=/root/lede-sdk-gcc-5.4.0/staging_dir/toolchain-mips_24kc_gcc-5.4.0_musl-1.1.16/bin/mips-openwrt-linux-

export CROSS_COMPILE=$(CONFIG_TOOLCHAIN_PREFIX)
export MAKECMD=make ARCH=mips

export UBOOT_GCC_4_3_3_EXTRA_CFLAGS=-fPIC

export COMPRESSED_UBOOT=0

ifeq ($(BOARD_TYPE),ap121)
export UBOOT_CONFIG_TARGET=board956x
export CFG_BOARD_NAME=ALICLOUD AP121
export AH_CAL_IN_FLASH_PCI=1
export AR7240_EMU=1
export ATH_CFG_SGMII=1
export ATH_CONFIG_SWAP_SGMII=0
export ATH_GMAC0_MII=ATHR_SGMII
export ATH_GMAC1_MII=ATHR_SGMII
export ATH_GMAC_GE0_IS_CONNECTED=1
export ATH_S17_MAC0_SGMII=1
export ATH_SGMII_FORCED=1
export BIG_ENDIAN_HOST=1
export BUILD_CONFIG=_vlan
export BUILD_TYPE=jffs2
export BUS=dual
export CFG_BOARD_TYPE=ap152
export ddr_cas=5
export ETH_CONFIG=_s17
export FLASH_SIZE=16
export pll=CFG_PLL_775_650_258
export GPIO_RESET=2
export GPIO_LED_STATUS=1
endif

ifeq ($(BOARD_TYPE),wr2220)
export UBOOT_CONFIG_TARGET=board956x
export CFG_BOARD_NAME=ALICLOUD WR2220
export AH_CAL_IN_FLASH_PCI=1
export AR7240_EMU=1
export ATH_CFG_SGMII=1
export ATH_CONFIG_SWAP_SGMII=0
export ATH_GMAC0_MII=ATHR_SGMII
export ATH_GMAC1_MII=ATHR_SGMII
export ATH_GMAC_GE0_IS_CONNECTED=1
export ATH_S17_MAC0_SGMII=1
export ATH_SGMII_FORCED=1
export BIG_ENDIAN_HOST=1
export BUILD_CONFIG=_vlan
export BUILD_TYPE=jffs2
export BUS=dual
export CFG_BOARD_TYPE=ap152
export ddr_cas=5
export ETH_CONFIG=_s17
export FLASH_SIZE=16
export pll=CFG_PLL_775_650_258
export GPIO_RESET=2
export GPIO_LED_STATUS=1
endif

ifeq ($(BOARD_TYPE),e8820)
export UBOOT_CONFIG_TARGET=board956x
export CFG_BOARD_NAME=ZTE E8820 v1
export AH_CAL_IN_FLASH_PCI=1
export AR7240_EMU=1
export ATH_CFG_SGMII=1
export ATH_CONFIG_SWAP_SGMII=0
export ATH_GMAC0_MII=ATHR_SGMII
export ATH_GMAC1_MII=ATHR_SGMII
export ATH_GMAC_GE0_IS_CONNECTED=1
export ATH_S17_MAC0_SGMII=1
export ATH_SGMII_FORCED=1
export BIG_ENDIAN_HOST=1
export BUILD_CONFIG=_vlan
export BUILD_TYPE=jffs2
export BUS=dual
export CFG_BOARD_TYPE=ap152
export ddr_cas=5
export ETH_CONFIG=_s17
export FLASH_SIZE=16
export pll=CFG_PLL_775_650_258
export GPIO_RESET=2
export GPIO_LED_STATUS=20
export GPIO_LED_GREEN=9
#export GPIO_LED_RED=14
endif

ifeq ($(BOARD_TYPE),e8821)
export UBOOT_CONFIG_TARGET=board956x
export CFG_BOARD_NAME=ZTE E8821
export CFG_BOARD_TYPE=ap152
export AH_CAL_IN_FLASH_PCI=1
export AR7240_EMU=1
export ATH_CFG_SGMII=1
export ATH_CONFIG_SWAP_SGMII=0
export ATH_GMAC0_MII=ATHR_SGMII
export ATH_GMAC1_MII=ATHR_SGMII
export ATH_GMAC_GE0_IS_CONNECTED=1
export ATH_S17_MAC0_SGMII=1
export ATH_SGMII_FORCED=1
export BIG_ENDIAN_HOST=1
export BUILD_CONFIG=_vlan
export BUILD_TYPE=jffs2
export BUS=dual
export ddr_cas=5
export ETH_CONFIG=_s17
export FLASH_SIZE=1
export ATH_DUAL_FLASH=1
export ATH_SPI_NAND=1
export ATH_SPI_NAND_CS_GPIO=6
export pll=CFG_PLL_775_650_258
export GPIO_RESET=2
export GPIO_LED_STATUS=20
export GPIO_LED_GREEN=9
export GPIO_LED_RED=14
endif

ifeq ($(BOARD_TYPE),wdr8500)
export UBOOT_CONFIG_TARGET=board956x
export CFG_BOARD_NAME=TP-LINK WDR8500 V1
export AH_CAL_IN_FLASH_PCI=1
export AR7240_EMU=1
export ATH_CFG_SGMII=1
export ATH_CONFIG_SWAP_SGMII=0
export ATH_GMAC0_MII=ATHR_SGMII
export ATH_GMAC1_MII=ATHR_SGMII
export ATH_GMAC_GE0_IS_CONNECTED=1
export ATH_S17_MAC0_SGMII=1
export ATH_SGMII_FORCED=1
export BIG_ENDIAN_HOST=1
export BUILD_CONFIG=_vlan
export BUILD_TYPE=jffs2
export BUS=dual
export CFG_BOARD_TYPE=ap152
export ddr_cas=5
export ETH_CONFIG=_s17
export FLASH_SIZE=16
export pll=CFG_PLL_775_650_258
export GPIO_RESET=1
export GPIO_LED_STATUS=21
export GPIO_LED_GREEN=5
#export GPIO_LED_RED=14
endif

ifeq ($(BOARD_TYPE),ap720)
export UBOOT_CONFIG_TARGET=board956x
export CFG_BOARD_NAME=RUIJIE AP720-L
export AH_CAL_IN_FLASH_PCI=1
export AR7240_EMU=1
export ATH_CFG_SGMII=1
export ATH_CONFIG_SWAP_SGMII=0
export ATH_GMAC0_MII=ATHR_SGMII
export ATH_GMAC1_MII=ATHR_SGMII
export ATH_GMAC_GE0_IS_CONNECTED=1
export ATH_S17_MAC0_SGMII=1
export ATH_SGMII_FORCED=1
export BIG_ENDIAN_HOST=1
export BUILD_CONFIG=_vlan
export BUILD_TYPE=jffs2
export BUS=dual
export CFG_BOARD_TYPE=ap152
export ddr_cas=5
export ETH_CONFIG=_s17
export FLASH_SIZE=16
export pll=CFG_PLL_775_650_258
export GPIO_RESET=2
export GPIO_LED_STATUS=7
endif

TARGETS=ap121 wr2220 e8820 e8821 wdr8500 ap720

IMAGEPATH=$(BUILD_TOPDIR)/bin
UBOOT_BINARY=u-boot.bin
UBOOTFILE=$(BOARD_TYPE)_u-boot.bin

all:
ifeq (,$(filter $(BOARD_TYPE), $(TARGETS)))
	@echo "Run 'make BOARD_TYPE=<target_name>'"
	@echo "Available targets: $(TARGETS)"
else
	cd $(UBOOTDIR) && $(MAKECMD) $(UBOOT_CONFIG_TARGET)_config
	cd $(UBOOTDIR) && $(MAKECMD) ENDIANNESS=-EB V=1 all
	@echo Copy binaries to $(IMAGEPATH)/$(UBOOTFILE)
	mkdir -p $(IMAGEPATH)
	cp -f $(UBOOTDIR)/$(UBOOT_BINARY) $(IMAGEPATH)/$(UBOOTFILE)
	@make clean
	@echo Done
endif
	
clean:
	cd $(UBOOTDIR) && $(MAKECMD) distclean